package com.xingxing.controller;

import com.xingxing.entity.StatusEntity;
import com.xingxing.service.AliyunMessageService;
import com.xingxing.service.JunkMailService;
import com.xingxing.service.UserService;
import com.xingxing.utils.CommUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author: 小白一号
 * @date: 2022/11/18
 * @desc:
 */
@Slf4j
@RestController
@RequestMapping("/verifyCode")
public class VerifyCode {
    @Autowired
    private AliyunMessageService aliyunMessageService;
    @Autowired
    private JunkMailService junkMailService;
    @Autowired
    private UserService userService;

    @PostMapping("/createRegisterPhoneVerify")
    public Object createRegisterPhoneVerify(@RequestBody Map<String, Object> map) throws Exception {
        StatusEntity status = new StatusEntity();
        try {
            if (userService.findByUserPhone(map.get("phone").toString()) != null) {
                status.setStatus("userExist");
                status.setCode("302");
            } else {
                String verifyCode = CommUtils.randomVeryCode();
                if (aliyunMessageService.sendVerifyCode(map.get("phone").toString(), verifyCode)) {
                    CommUtils.VeryCodeMap.put(map.get("phone").toString(), verifyCode);
                    status = new StatusEntity("success", "200");
                } else {
                    status = new StatusEntity("error", "302");
                }
            }
        } catch (Exception e) {

        }
        return status;
    }

    @PostMapping("/createRegisterEmailVerify")
    public Object createRegisterEmailVerify(@RequestBody Map<String, Object> map) throws Exception {
        StatusEntity status = new StatusEntity();
        try {
            if (userService.findByUserEmail(map.get("email").toString()) != null) {
                status.setStatus("userExist");
                status.setCode("302");
            } else {
                String verifyCode = CommUtils.randomVeryCode();
                if (junkMailService.sendVerifyCode(map.get("email").toString(), verifyCode)) {
                    status = new StatusEntity("success", "200");
                    CommUtils.VeryCodeMap.put(map.get("email").toString(), verifyCode);
                } else {
                    status = new StatusEntity("error", "302");
                }
            }
        } catch (Exception e) {

        }

        return status;
    }

    @PostMapping("/createPhoneVerify")
    public Object phoneVirifyCode(@RequestBody Map<String, Object> map) throws Exception {
        StatusEntity status = new StatusEntity();
        String verifyCode = CommUtils.randomVeryCode();
        if (aliyunMessageService.sendVerifyCode(map.get("phone").toString(), verifyCode)) {
            CommUtils.VeryCodeMap.put(map.get("phone").toString(), verifyCode);
            status = new StatusEntity("success", "200");
        } else {
            status = new StatusEntity("error", "302");
        }
        return status;
    }

    @PostMapping("/createEmailVerify")
    public Object emailVirifyCode(@RequestBody Map<String, Object> map) throws Exception {
        StatusEntity status = new StatusEntity();

        String verifyCode = CommUtils.randomVeryCode();
        if (junkMailService.sendVerifyCode(map.get("email").toString(), verifyCode)) {
            status = new StatusEntity("success", "200");
            CommUtils.VeryCodeMap.put(map.get("email").toString(), verifyCode);
        } else {
            status = new StatusEntity("error", "302");
        }
        return status;
    }

    @PostMapping("/verifyCode")
    public Object verifyCode(@RequestBody Map<String, Object> map) {
        StatusEntity status;
        try {
            String codeFromMap = CommUtils.VeryCodeMap.get(map.get("phoneOrEmail").toString());
            if (codeFromMap != null) {
                if (codeFromMap.equals(map.get("Code").toString())) {
                    status = new StatusEntity("success", "200");
                } else {
                    status = new StatusEntity("error", "302");
                }
            } else {
                status = new StatusEntity("empty", "301");
            }
        } catch (Exception e) {
            status = new StatusEntity("paramError", "303");
        }
        return status;
    }

}
